<?php
/**
 * 礼物记录
 * @return
 * @author yuncran@126.com
 */

namespace app\model;

use app\BaseModel;
use app\services\UserService;

class BuyGiftLogModel extends BaseModel
{
    protected $name = 'buy_gift_log';

    protected $pk = "id";

    protected $append = ['gift_txt'];

    public static function getGiftBuyAll($user_id)
    {
        $data = self::getModel()->with(['user','gift'])->withoutField(['update_time'])->where(['to_user_id' => $user_id])->order('id','DESC')->paginate()->each(function ($item) {
            return $item;
        });
        return $data;
    }

    public static function getUserGiftBuyAll($user_id)
    {
        $data = self::getModel()->with(['user','gift'])->withoutField(['update_time'])->where(['user_id' => $user_id])->order(['id'=>'DESC'])->paginate()->each(function ($item) {
            return $item;
        });
        return $data;
    }

    public function getGiftTxtAttr($value)
    {
        return $this->hasOne(GiftModel::class, 'id', 'gift_id')->value(field('title'));
    }

    public function user()
    {
        return $this->hasOne(UserModel::class,'id','to_user_id')->bind(['user_nickname'=>'nickname']);
    }

    public function gift()
    {
        return $this->hasOne(GiftModel::class,'id','gift_id')->bind(['gift_image'=>'image']);
    }

    /**
     * 处理新增事件 || 增加系统消息通知
     * @param $trends_comment
     * @return void
     * @author yuncran@126.com
     */
//    public static function onAfterInsert($trends_comment)
//    {
//        $trends = TrendsModel::where('id', $trends_comment->trends_id)->field('id,user_id')->find();
//        return UserService::noticeUserCheckAuth($trends_comment->user_id, $trends->user_id,$trends_comment->trends_id);
//    }

}